package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.AftersaleOutDepotCriteria;
import com.javasm.domin.entity.AftersaleOutDepot;
import com.javasm.domin.vo.AftersaleOutDepotVo;
import com.javasm.mapper.AftersaleOutDepotMapper;
import com.javasm.service.AftersaleOutDepotService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.AftersaleOutDepotTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ljy
 * @since 2022-01-16
 */
@Service
@Transactional
@RequiredArgsConstructor
public class AftersaleOutDepotServiceImpl extends BaseServiceimpl<AftersaleOutDepot> implements AftersaleOutDepotService {

    private final AftersaleOutDepotMapper aftersaleOutDepotMapper;

    private final AftersaleOutDepotTransfer aftersaleOutDepotTransfer;

    @Override
    public AftersaleOutDepotVo castToVo(Long id) {

        AftersaleOutDepot aftersaleOutDepot = aftersaleOutDepotMapper.selectById(id);
        AftersaleOutDepotVo aftersaleOutDepotVo = aftersaleOutDepotTransfer.toVO1(aftersaleOutDepot);

        return aftersaleOutDepotVo;
    }

    @Override
    public PageResult<AftersaleOutDepotVo> selectByPageCriteria(AftersaleOutDepotCriteria aftersaleOutDepotCriteria) {

        PageHelper.startPage(aftersaleOutDepotCriteria.getCurrentPage(), aftersaleOutDepotCriteria.getPageSize());
        LambdaQueryWrapper<AftersaleOutDepot> lambda = new QueryWrapper<AftersaleOutDepot>().lambda();

        if (aftersaleOutDepotCriteria.getId() != null && aftersaleOutDepotCriteria.getId() != 0) {
            lambda.eq(AftersaleOutDepot::getId, aftersaleOutDepotCriteria.getId());
        }

        if (aftersaleOutDepotCriteria.getOutType() != null && aftersaleOutDepotCriteria.getOutType() != 0) {
            lambda.eq(AftersaleOutDepot::getOutType, aftersaleOutDepotCriteria.getOutType());
        }

        if (aftersaleOutDepotCriteria.getOutStatus() != null) {
            lambda.eq(AftersaleOutDepot::getOutStatus, aftersaleOutDepotCriteria.getOutStatus());
        }

        if (!StringUtils.isEmpty(aftersaleOutDepotCriteria.getStartTime())) {
            lambda.between(AftersaleOutDepot::getOutTime, aftersaleOutDepotCriteria.getStartTime(), aftersaleOutDepotCriteria.getEndTime());
        }

        List<AftersaleOutDepot> aftersaleOutDepots = aftersaleOutDepotMapper.selectList(lambda);


        PageInfo<AftersaleOutDepot> pageInfo = new PageInfo<>(aftersaleOutDepots);
        List<AftersaleOutDepotVo> aftersaleOutDepotVos = aftersaleOutDepotTransfer.toVO(aftersaleOutDepots);

        return new PageResult<AftersaleOutDepotVo>(pageInfo.getTotal(), aftersaleOutDepotVos);
    }
}
